<template>
  <div class="edit-task">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>更新任务</span>
      </div>
      <el-form :model="taskForm" label-width="100px">
        <el-form-item label="任务名称">
          <el-input v-model="taskForm.name"></el-input>
        </el-form-item>
        <el-form-item label="完成状态">
          <el-switch v-model="taskForm.completed"></el-switch>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="updateTask">更新</el-button>
          <el-button @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      taskForm: {
        id: null,
        name: '',
        completed: false
      }
    };
  },
  methods: {
    updateTask() {
      const token = localStorage.getItem('token');
      axios.put('/api/tasks/update', this.taskForm, {
        headers: {
          'Authorization': token
        }
      })
      .then(() => {
        this.$message.success('任务已更新');
        // 根据你的业务逻辑进行跳转或其他操作
      })
      .catch(error => {
        this.$message.error('更新任务失败');
        console.error('Error:', error);
      });
    },
    cancel() {
      // 根据你的业务逻辑进行取消操作
    }
  }
};
</script>

<style scoped>
.update-task {
  margin: 20px;
}
.box-card {
  width: 400px;
}
</style>
